# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("//bazel:pl_build_system.bzl", "pl_cc_bpf_test", "pl_cc_library", "pl_cc_test")
load("//src/stirling/source_connectors/perf_profiler/testing:testing.bzl", "agent_libs", "jdk_names", "px_jattach", "stirling_profiler_java_args")

package(default_visibility = ["//src/stirling:__subpackages__"])

pl_cc_library(
    name = "cc_library",
    srcs = glob(
        ["*.cc"],
        exclude = [
            "**/*_test.cc",
        ],
    ),
    hdrs = glob(["*.h"]),
    deps = [
        "//src/stirling/core:cc_library",
        "//src/stirling/source_connectors/perf_profiler/bcc_bpf:profiler",
        "//src/stirling/source_connectors/perf_profiler/bcc_bpf_intf:cc_library",
        "//src/stirling/source_connectors/perf_profiler/symbolizers:cc_library",
        "//src/stirling/utils:cc_library",
    ],
)

jdk_image_names = ["%s-java-profiler-test-image" % jdk_name for jdk_name in jdk_names]

image_tars = ["//src/stirling/source_connectors/perf_profiler/testing/java:%s-java-profiler-test-image.tar" % jdk_name for jdk_name in jdk_names]

pl_cc_bpf_test(
    name = "perf_profiler_bpf_test",
    timeout = "long",
    srcs = ["perf_profiler_bpf_test.cc"],
    args = stirling_profiler_java_args,
    data = agent_libs + image_tars + [
        px_jattach,
        "//src/stirling/source_connectors/perf_profiler/testing/cc:profiler_test_app_fib",
        "//src/stirling/source_connectors/perf_profiler/testing/go:profiler_test_app_sqrt_go",
        "//src/stirling/source_connectors/perf_profiler/testing/java:graal-vm-aot-test",
        "//src/stirling/source_connectors/perf_profiler/testing/java:profiler_test",
    ],
    defines = ['JDK_IMAGE_NAMES=\\"%s\\"' % ",".join(jdk_image_names)],
    env = {"QEMU_CPU_COUNT": "4"},
    shard_count = 3,
    tags = [
        "cpu:16",
        "no_qemu",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/common/exec:cc_library",
        "//src/common/testing/test_utils:cc_library",
        "//src/stirling/source_connectors/perf_profiler/testing:cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_bpf_test(
    name = "stringifier_bpf_test",
    srcs = ["stringifier_bpf_test.cc"],
    tags = [
        "cpu:16",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/perf_profiler/testing/bpf:stringifier_test_bpf_text",
    ],
)

pl_cc_test(
    name = "stack_trace_id_cache_test",
    srcs = ["stack_trace_id_cache_test.cc"],
    deps = [
        ":cc_library",
    ],
)
